package PrinterSource;

import Backend.PrinterAgent;
import Backend.SnmpAgent;
import Backend.SnmpManager;
import Models.PrinterModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.smi.OID;

public class PrinterManager {
    
    static Logger log = Logger.getLogger("SNMPLOG");
    SnmpManager manager;
    public ArrayList<PrinterModel> models;
    int PrinterNumbers = 5;
    
    public PrinterManager() throws IOException {
        
        models = new ArrayList<PrinterModel>();
        CreatePrinters();
    }
    
    public void CreatePrinters() throws IOException {
        
        String status = "GREEN";
        //String devicename = "Printer";
        Random rand = new Random();
        
        ArrayList<String> ips = new ArrayList<String>();
        ArrayList<String> communities = new ArrayList<String>();
        for (int i = 0; i < PrinterNumbers; i++) {
            
            
            models.add(new PrinterModel(rand, i, Integer.toString(2001 + i), "Printer " + i, rand.nextInt(2000000), status, rand.nextInt(250)));
            if (models.size() > i) {
                models.get(i).setjob(rand.nextInt(10));
                ips.add(models.get(i).getIP());
                communities.add(SnmpAgent.COMMUNITY);
            } else {
                log.error("Could not set model, no model " + i + " found");
            }
        }
        manager = new SnmpManager(ips, communities);
        manager.start();
    }
    
    public int getDevice(String name) {
        
        if (models != null) {
            for (int i = 0; i < models.size(); i++) {
                try {
                    if (manager.getString(i, PrinterAgent.name).matches(name)) {
                        return i;
                    }
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(PrinterManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            log.error("No object found with name " + name + " in printer list " + Thread.currentThread().getStackTrace());
        } else {
            log.error("Models Arraylist is NULL: " + Thread.currentThread().getStackTrace());
        }
        
        return models.size() + 1;
        
    }
    
    public ArrayList<String[]> getNetworkDetails() {
        if (models != null) {
            ArrayList<String[]> network = new ArrayList<String[]>();
            for (int i = 0; i < models.size(); i++) {
                try {
                    String[] data = new String[5];
                    data[0] = manager.getString(i, PrinterAgent.name);
                    data[1] = models.get(i).getIP();
                    data[2] = models.get(i).Get_Port();
                    data[3] = manager.getString(i, PrinterAgent.status);
                    data[4] = manager.getString(i, PrinterAgent.jobs);                    
                    network.add(data);
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(PrinterManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return network;
        } else {
            log.error("Models Arraylist is NULL: " + Thread.currentThread().getStackTrace());
        }
        return null;
    }
    
    public String[] getDetails(int index) {
        
        try {
            return manager.getArrayStrings(index, PrinterAgent.colourlist);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(PrinterManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public String getData(int index, OID oid) {
        try {
            return manager.getString(index, oid);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(PrinterManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
    
    public String setData(int index, OID oid, String val) {
        try {
            manager.set(index, oid, val, new ResponseListener() {
                public void onResponse(ResponseEvent event) {
                    PDU strResponse;
                    ((Snmp) event.getSource()).cancel(event.getRequest(), this);
                    strResponse = event.getResponse();
                    if (strResponse != null) {
                        log.info("Received response PDU is: " + strResponse.toString());
                    }
                }
            });
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(PrinterManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
    
    public PrinterModel SetModel(PrinterModel e) {
        
        if (models != null) {
            this.models.add(e);
        } else {
            System.out.println("ERROR setting Model, Arraylist NULL: " + Thread.currentThread().getStackTrace());
        }
        
        return null;
        
    }
}
